﻿function SiteAttribute(newData) {
    this.id = ko.observable(newData.id);
    this.Name = ko.observable(newData.Name);
    this.SiteId = ko.observable(newData.SiteId);
}

function SiteAttributesViewModel() {

    // Data
    var self = this;
    self.actualSiteId = ko.observable(data.SiteId);
    self.siteAttributes = ko.observableArray(data.SiteAttributes);
    self.newNameText = ko.observable();
    self.saHub = $.connection.siteAttributesHub;

    // Operations
    self.addSiteAttribute = function () {
        self.saHub.server.addSiteAttribute(data.SiteId, self.newNameText());
    }

    self.saHub.client.confirmAddSiteAttribute = function (saId) {
        self.siteAttributes.push(new SiteAttribute({ id: saId, Name: self.newNameText(), SiteId: data.SiteId }));
        self.newNameText("");
    }

    self.editSiteAttribute = function (sa) {
        self.saHub.server.editSiteAttribute(sa.id, sa.Name);
    }

    self.removeSiteAttribute = function (sa) {
        self.fsHub.server.deleteSiteAttribute(sa.id).done(function () {
            self.siteAttributes.destroy(sa);
        });
    };
};

var saVM = new SiteAttributesViewModel();
ko.applyBindings(saVM);

$.connection.hub.start();
